package com.ht.demo.admin.controller.system;

import com.ht.demo.common.appexception.ResultData;
import com.ht.demo.service.redis.RedisKeyUtil;
import com.ht.demo.service.redis.RedisUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

/**
 * @Author hdengxian
 * @Date 2023/5/24 13:50
 */
@RestController
@RequestMapping("${basePath}")
public class SysOnlineUserController {
    @Autowired
    private RedisUtil redis;

    @GetMapping("/online/number")
    public ResultData<Integer> getOnlineUserNum() {
        Collection<String> keys = redis.getKeys(RedisKeyUtil.LOGIN_TOKEN_KEY + "*");
        if (CollectionUtils.isEmpty(keys)) {
            return new ResultData<>(0);
        } else {
            return new ResultData<>(keys.size());
        }
    }

}
